/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.*;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.sound.midi.Sequence;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import java.awt.image.ColorConvertOp;
import java.awt.color.ColorSpace;
import javax.sound.sampled.AudioInputStream;

import sound.*;
/**
 * base class for resource managment.
 * Loads 
 *
 * @author Yati
 */
public class ResourceManager {
    
    private GraphicsConfiguration gc;
    private SoundManager soundManager;
    private MidiPlayer midiPlayer;
    
    public ResourceManager(GraphicsConfiguration gc,SoundManager soundManager,MidiPlayer midiPlayer){
        this.gc = gc;
        this.soundManager = soundManager;
        this.midiPlayer = midiPlayer;
    }
    
        /**
        Gets an image from the images/ directory.
    */
    public Image loadImage(String name) {
        String filename = "images/" + name;
        return new ImageIcon(filename).getImage();
    }
    
    public Image makeMasked(URL path, Color color){
        BufferedImage image=null;
        //String dir=System.getProperty("user.dir");
        //URL url=getClass().getResource(dir+"/"+path);

        //URL test=getClass().getResource("images/0001.png");

        /*if(test==null){
            System.out.print("hi yo");
        }*/


        try{
            image=ImageIO.read(path);

        }catch(IOException e){
            System.err.println("This is the error");
            e.printStackTrace();
        }

        BufferedImage dimg= new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_ARGB);

        Graphics2D g= dimg.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.drawImage(image, null, 0,0);
        g.dispose();
        for(int i=0;i<dimg.getHeight();i++){
            for(int j=0;j<dimg.getWidth();j++){
                if(dimg.getRGB(j, i)==color.getRGB()){
                    dimg.setRGB(j, i, 0x8F1C1C);
                }
            }
        }

        return Toolkit.getDefaultToolkit().createImage(dimg.getSource());

    }
    

    public Image makeGrayScale(BufferedImage original){
         ColorConvertOp grayScaleFilter= new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
         Image grayScaleImage=null;

         if(original!=null){
            grayScaleImage=grayScaleFilter.filter(original, null);

        }
         return grayScaleImage;
    }
    
        /**
     * Converts an Image into BufferedImage
     *
     * @param imageIn
     * @return bufferImage version of imageIn
     */
    protected BufferedImage convertImageToBuffered(Image imageIn){
        //MediaTracker mt = new MediaTracker(comp);
        //mt.addImage(imageIn, 0);
        //try {
        //  mt.waitForID(0);
        //} catch (InterruptedException ie) {
        //}
        BufferedImage bufferedImageOut = new BufferedImage(imageIn
            .getWidth(null), imageIn.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics g = bufferedImageOut.getGraphics();
        g.drawImage(imageIn, 0, 0, null);

        return bufferedImageOut;
    }
    
     public Image getMirrorImage(Image image) {
        return getScaledImage(image, -1, 1);
    }


    public Image getFlippedImage(Image image) {
        return getScaledImage(image, 1, -1);
    }


    private Image getScaledImage(Image image, float x, float y) {

        // set up the transform
        AffineTransform transform = new AffineTransform();
        transform.scale(x, y);
        transform.translate(
            (x-1) * image.getWidth(null) / 2,
            (y-1) * image.getHeight(null) / 2);

        // create a transparent (not translucent) image
        Image newImage = gc.createCompatibleImage(
            image.getWidth(null),
            image.getHeight(null),
            Transparency.TRANSLUCENT);

        // draw the transformed image
        Graphics2D g = (Graphics2D)newImage.getGraphics();
        g.drawImage(image, transform, null);
        g.dispose();

        return newImage;
    }
    
        public URL getResource(String filename) {
        return getClass().getClassLoader().getResource(filename);
    }

    public InputStream getResourceAsStream(String filename) {
        return getClass().getClassLoader().
            getResourceAsStream(filename);
    }


    public Sound loadSound(String name) {
        return soundManager.getSound(getResourceAsStream(name));
    }
    
    public AudioInputStream loadSoundAsStream(String name){
        
        return soundManager.loadAudio(name);
    };


    public Sequence loadSequence(String name) {
        return midiPlayer.getSequence(getResourceAsStream(name));
    }

    
    
}
